﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ValidateCommon;
using BusinessLogic.ClassFactory;
using BusinessLogic.BusinessRules;
using System.Data;
using Common.Xml;

namespace RichECMS.ServerPages
{
    public partial class Main : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["Wid"] != null)
            {
                if (ParamAuth.IsNumber(Request["Wid"].ToString().Trim()))
                {
                    using (UserWebRelationFactory objUserWebRelationFactory = new UserWebRelationFactory())
                    {
                        UserWebRelation objUserWebRelation = objUserWebRelationFactory.CreatorBySql("chvLoginName='" + State.LoginUser.LoginName + "' and intWSID=" + Request["Wid"].ToString().Trim());
                        if (objUserWebRelation != null)
                        {
                            if (!State.IsManager)
                            {
                                UserWebRelation objUserWebRelationguest = objUserWebRelationFactory.CreatorBySql("chvLoginName='" + State.LoginUser.LoginName + "' and intWSID=" + Request["Wid"].ToString().Trim() + " and intState=1");
                                if (objUserWebRelationguest != null)
                                {
                                    State.intWSID = objUserWebRelationguest.DataItem.intWSID;
                                }
                                else
                                {
                                    Response.Write("您没有权限访问");
                                    Response.End();
                                }
                            }
                            else
                            {
                                State.intWSID = Convert.ToInt32(Request["Wid"].ToString().Trim());
                            }


                            //是否启用父路径
                            XMLProcess objXMLProcess = new XMLProcess("Setting\\GlobalSet.config");
                            string enableParentPath = objXMLProcess.ReadAttribute("/configuration/GlobalSet/ParentPath", "Enable");
                            if (enableParentPath.Trim().ToLower() == "true")
                            {
                                State.LoginUser.EnableParentPath = true;
                                //启用父路径情况下是否启用栏目ID
                                string enableSectionID = objXMLProcess.ReadAttribute("/configuration/GlobalSet/ParentPath", "EnableSectionID");
                                if (enableSectionID.Trim().ToLower() == "true")
                                {
                                    State.LoginUser.EnableSectionID = true;
                                }

                                //是否启用短路径
                                string enableShortPath = objXMLProcess.ReadAttribute("/configuration/GlobalSet/ShortPath", "Enable");
                                if (enableShortPath.Trim().ToLower() == "true")
                                {
                                    State.LoginUser.EnableShortPath = true;
                                }
                            }

                            //获取网站等级
                            WebSiteFactory objWebSiteFactory = new WebSiteFactory();
                            WebSite objWebSite = objWebSiteFactory.Creator(objUserWebRelation.DataItem.intWSID);
                            if (objWebSite != null)
                            {
                                State.LoginUser.WebSiteLevel = objWebSite.DataItem.IsintWebSiteLevelNull() ? 0 : objWebSite.DataItem.intWebSiteLevel;
                            }

                            //读取发布设置
                            using (SystemVariableFactory objSystemVariableFactory = new SystemVariableFactory())
                            {
                                SystemVariable objSystemVariable = objSystemVariableFactory.CreatorBySql("intWSID=" + State.intWSID);
                                if (objSystemVariable != null)
                                {
                                    string strRepleaceOld = "serverpages\\main.aspx".ToLower();
                                    string strwebpath = Server.MapPath(Request.Path).ToLower().Replace(strRepleaceOld, "");

                                    State.LoginUser.WebSiteID = State.intWSID;
                                    State.LoginUser.VartualPath = objSystemVariable.DataItem.chvIssuePath;
                                    State.LoginUser.Rootpath = strwebpath;

                                    DataTable tblTmp = objUserWebRelation.Data.Tables[0];
                                    DataRow row = tblTmp.Rows[0];
                                    //记录权限
                                    State.LoginUser.ManagerCompetence = objUserWebRelation.DataItem.intManager;
                                    State.LoginUser.Permissions = row["chvPermissions"] != DBNull.Value ? objUserWebRelation.DataItem.chvPermissions : "";
                                    State.LoginUser.PermissionsText = row["chvPermissionsText"] != DBNull.Value ? objUserWebRelation.DataItem.chvPermissionsText : "";
                                    State.LoginUser.StrName = row["chvUserName"] != DBNull.Value ? objUserWebRelation.DataItem.chvUserName : "";

                                    State.LoginUser.IssuePath = strwebpath + (State.LoginUser.EnableParentPath ? "" : ("news\\" + objSystemVariable.DataItem.chvIssuePath + "\\"));
                                    State.LoginUser.MiniPicPath = strwebpath + "Pcfiles\\" + objSystemVariable.DataItem.chvMiniPicPath;
                                    State.LoginUser.PicPath = strwebpath + "Pcfiles\\" + objSystemVariable.DataItem.chvPicPath;
                                    State.LoginUser.WebUrl = objSystemVariable.DataItem.chvWebUrl;
                                    State.LoginUser.TemplatePath = strwebpath + "TemplateFile\\" + objSystemVariable.DataItem.chvTemplatePath;
                                }
                            }
                        }
                        else
                        {
                            Response.Write("您访问的数据不存在");
                            Response.End();
                        }
                    }
                }
                else
                {
                    Response.Write("参数错误");
                    Response.End();
                }
            }
            else
            {
                Response.Write("参数错误");
                Response.End();
            }
        }
    }
}